<template>
  <div class="edit_data">
    <!-- 顶部导航栏 -->
    <head-nav-bar>
      <template #left>
        <div class="head_name" @click="back">
          <img
            class="return"
            src="https://www.hualigs.cn/image/60408d2367fba.jpg"
            alt=""
          />
          <span class="return_">返回</span>
        </div>
      </template>
      <template #center>
        <div class="head_name2">{{ name }}</div>
      </template>
    </head-nav-bar>
    <div class="content"></div>

    <div class="room_chat">
      <div id="room">
        <div class="clear"></div>
        <info :list="list" />
      </div>
    </div>

    <!-- 页底 -->
    <div class="footer">
      <div class="footer_left">
        <img
          class="footer_img"
          src="https://www.hualigs.cn/image/6040ea1c3b41f.jpg"
          alt=""
        />
      </div>
      <div class="footer_center">
        <input
          v-model="infomation"
          type="text"
          class="footer_center_edit"
          @keyup.enter="submit"
        />
      </div>
      <div class="footer_right">
        <img
          class="footer_img"
          src="https://www.hualigs.cn/image/6040ea1c3bd9f.jpg"
          alt=""
        />
      </div>
    </div>
  </div>
</template>
<script>
import http from "../../API/http"
import info from "../../components/chatroom/info";
import HeadNavBar from "../HeadNavBar";
export default {
  data() {
    return {
      infomation: "",
      name: null,
      id: "", //当前正在聊天对象的socketID
      list: [],
      userid : ""
    };
  },
  components: {
    info,
    HeadNavBar,
  },
  sockets: {
    receiveMsg(data) {
      data.name === this.$store.state.Authorization ? (data.tag = 1) : (data.tag = 0);
      this.list.push(data);

      let datas = this.$store.state.userinfo.userList;
      var flag = true;
      if (datas.length === 0) {
        this.$store.commit("setuserList", data);
      } else {
        for (let i = 0; i < datas.length; i++) {
          if (datas[i].userid === data.userid) {
            datas[i].content = data.content;
            flag = false;
            break;
          }
        }
        if (flag === true) {
          this.$store.commit("setuserList", data);
        }
      }
    },
  },
  async created() {
    // console.log(this.$route);
    this.name = this.$route.params.id;
    this.id = this.$route.params.name;
    this.userid = this.$route.params.userid;
    let {data} = await http._getChat({
      "uid" : this.$store.state.ID,
      "toUid" : this.$route.params.userid
    })
    console.log(data.userSayList,data.toUserSayList)
    // 自己的信息
    for(let i=0;i<data.userSayList.length;i++){
      data.userSayList[i].img = "https://www.hualigs.cn/image/60364a03036ce.jpg";
      data.userSayList[i].name = this.$store.state.Authorization;
      data.userSayList[i].tag = 1;
      data.userSayList[i].id = this.$store.state.socketID;
    }
    for(let p=0;p<data.toUserSayList.length;p++){
      data.toUserSayList[p].img = "https://www.hualigs.cn/image/60364a03036ce.jpg";
      data.toUserSayList[p].name = this.$route.params.id;
      data.toUserSayList[p].tag = 0;
      data.toUserSayList[p].id = this.$route.params.name;
    }
    let arr = data.userSayList.concat(data.toUserSayList)
    // 进行冒泡排序
    for(var i=0;i<arr.length-1;i++){//确定轮数
      for(var j=0;j<arr.length-i-1;j++){//确定每次比较的次数
        if(arr[j].time*1 > arr[j+1].time*1){
          let tem = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = tem;
        }
      }
    }
    // console.log(arr)
    this.list = arr

  },
  methods: {
    back() {
      this.$router.go(-1); //返回上一层
    },
    async submit() {
      if (this.infomation !== null) {
        let obj = {
          name: this.$store.state.Authorization,
          img: "https://www.hualigs.cn/image/60364a03036ce.jpg",
          tag: 1,
          content: this.infomation,
          id: this.$store.state.socketID,
          time: Date.now(),
          color : "#DC143C",
          userid : this.$store.state.ID,
        };
        this.list.push(obj);
        this.$socket.emit(
          "sendOnlyPeople",
          this.$store.state.socketID,
          this.id,
          obj
        );
        let {msg,status} = await http._saveChat({
          "uid":this.$store.state.ID,
          "toUid" : this.$route.params.userid,
          "content" : this.infomation
        })
        // console.log(msg,status)

      }
      this.infomation = "";
    },
  },
};
</script>
<style scoped>
.room_chat {
  width: 100%;
  /* height: 100vh; */
  margin-bottom: 70px;
  padding: 20px 20px 20px 20px;
  background-color: #f3f3f3;
}
.room {
  position: relative;
  background-color: #f3f3f3;
  margin-bottom: 70px;
}
.clear {
  clear: both;
}
.edit_data {
  position: absolute;
  /* top: 0; */
  right: 0;
  /* bottom: 0; */
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #f3f3f3;
}
.head_name {
  position: absolute;
  height: 25px;
  width: 60px;
  line-height: 25px;
}
.return {
  position: absolute;
  top: 2px;
  height: 21px;
  width: 21px;
}
.return_ {
  position: absolute;

  top: 0;
  left: 22px;
  height: 25px;
  line-height: 25px;
}
.head_name2 {
  margin: 0 auto;
}
.footer {
  height: 7vh;
  width: 100%;
  position: fixed;
  bottom: 0;
  right: 0;
  left: 0;
  background-color: #ffffff;
  border-top: 1px solid #d8d8d8;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
/* .footer_left{

} */
.footer_center {
  width: 74%;
  background-color: #f3f3f3;
  height: 5vh;
}
/* .footer_right{

} */
.footer_img {
  height: 5vh;
}
.footer_center_edit {
  width: 100%;
  background-color: #f3f3f3;
  height: 5vh;
  border: none;
  padding: 0 10px 0 10px;
  outline-style: none;
}
</style>
